const app = getApp()

Page({
  data: {
    id: 0,
    buyNum: 1, //购买数量
    goods_img: '', //商品图片地址
    order_amount: 0, //商品单价
    goods_name: '', //商品名称
    pay_number: 1,
    starIndex1: 0,
    starIndex2: 0,
    starIndex3: 0,
    order_number: '',
    head_img: '', //店铺头像
    merchants_name: '', //商家name
    merchants_subtitle: '', //商家描述
    tagList: [],
    tagsArr: [], //评价标签列表
    dictId: [], //评价标签id
    comments: [] //选中的评价
  },
  onLoad(options) {
    this.setData({
      id: options.id,
      order_number: options.ordernum,
      buyNum: options.buyNum,
      goods_price: options.goods_price,
      paynum: options.pay_number
    })
    this.userEvaluation()
  },
  onShow() {

  },
  //用户评价
  userEvaluation(e) {
    var that = this;
    var token = wx.getStorageSync('token');
    wx.request({
      url: 'https://www.zbq888.cn/api/v1/user/order/to/evaluation',
      method: 'post',
      data: {
        order_number: that.data.order_number
      },
      header: {
        'content-type': 'application/json',
        client: 1,
        token: token
      },
      success(res) {
        // console.log("res", res)
        var img_list = res.data.goods_img;
        var good_img = img_list.split(',');
        var tagList = res.data.dicts.map((item) => {
          item.isselected = false
          return item
        });
        var tagsArr = res.data.dicts.map((item) => {
          return item.code_name
        })

        that.setData({
          goods_img: good_img[0],
          goods_name: res.data.goods_name,
          pay_number: res.data.pay_number,
          order_amount: res.data.order_amount / 100,
          head_img: res.data.head_img,
          merchants_name: res.data.merchants_name,
          merchants_subtitle: res.data.merchants_subtitle,
          tagsArr: tagsArr,
          tagList: tagList
        })
      }
    })

  },
  //选择评价语
  tagsSelected(e) {
    var index = e.currentTarget.dataset.index;
    this.data.tagList[index].isselected = !this.data.tagList[index].isselected;
    this.setData({
      tagList: this.data.tagList
    })
  },
  //星星评分
  onChange(e) {
    var index = e.detail.index;
    if (e.currentTarget.dataset.type == 1) {
      this.setData({
        starIndex1: index
      })
    } else if (e.currentTarget.dataset.type == 2) {
      this.setData({
        starIndex2: index
      })
    } else {
      this.setData({
        starIndex3: index
      })
    }

  },
  // 提交用户评价
  getEvaluate() {
    var that = this;
    var token = wx.getStorageSync('token')
    let commentName = ''
    that.data.tagList.filter((item) => item.isselected).forEach((item) => {
      commentName = commentName + item.code_name + ","
    });
    if (commentName == '') {
      wx.showToast({
        title: 'title',
        icon:'none'
      })
    }
    commentName = commentName.substring(0, commentName.length - 1)
    wx.request({
      url: 'https://www.zbq888.cn/api/v1/user/order/evaluation',
      method:'post',
      data:{
        "comment_names":commentName,
        "grade_ids":'描述相符_' + that.data.starIndex1 +',服务态度_'+ that.data.starIndex2 +',物流服务_'+ that.data.starIndex3,
        "order_number": that.data.order_number
      },
      header:{
        'content-type':'application/json',
        client:1,
        token:token
      },
      success(res){
        // console.log(res)
      }
    })
  }
})